package com.yk.satoken.demos.minio.service;

import com.yk.satoken.demos.config.MinioStorageServiceImpl;
import com.yk.satoken.demos.minio.util.*;
import com.yk.satoken.demos.minio.vo.UploaderVO;
import io.minio.MinioClient;
import lombok.Cleanup;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/**
 * @author YangKai
 * @create 2024-06-02 15:02
 * @version: 1.0
 * @Description:
 */
@Service
public class FileService {


    @Autowired
    private MinioStorageServiceImpl minioStorageService;

    public UploaderVO uploaderVO(MultipartFile file) {
        String orgFileName = file.getOriginalFilename();
        String fileType = UpUtil.getFileType(file);
        String newFileName = UUID.randomUUID().toString().replaceAll("-", "").substring(0, 32) + "." + fileType;
        // 实际文件名
        String objectName = new SimpleDateFormat("yyyy/MM/dd/").format(new Date()) + newFileName;
        minioStorageService.uploadFile(file, objectName);
        return UploaderVO.builder()
                // 文件名称
                .filename(newFileName)
                // 原始文件名
                .originalFilename(orgFileName)
                // 存放路径地址
                .url(objectName)
                // 文件大小
                .fileSize(UpUtil.getFileSize(file))
                // 文件扩展名
                .ext(fileType)
                // 创建时间
                .createTime(cn.hutool.core.date.DateUtil.now()).build();
    }


 /*   public void downloadFileByPath(HttpServletResponse response, String path) {
        try (OutputStream output = response.getOutputStream(); InputStream input = minioStorageService.downloadFileByPath(path)) {
            response.setCharacterEncoding("utf-8");
            response.setContentType("multipart/form-data");
            IOUtils.copy(input, output);
        } catch (IOException e) {
            throw new RuntimeException("文件下载失败");
        }
    }*/
   /* public void downloadFileByPath(HttpServletResponse response, String path) {
        try (OutputStream output = response.getOutputStream(); InputStream input = minioStorageService.downloadFileByPath(path)) {
            String fileName = path.substring(path.lastIndexOf("/") + 1);
            String contentType = "application/octet-stream";
            if (fileName.endsWith(".png")) {
                contentType = "image/png";
            } else if (fileName.endsWith(".jpg") || fileName.endsWith(".jpeg")) {
                contentType = "image/jpeg";
            } else if (fileName.endsWith(".gif")) {
                contentType = "image/gif";
            } else if (fileName.endsWith(".bmp")) {
                contentType = "image/bmp";
            } else if (fileName.endsWith(".webp")) {
                contentType = "image/webp";
            }
            response.setCharacterEncoding("utf-8");
            response.setContentType(contentType);
            response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
            IOUtils.copy(input, output);
            FileDownloadUtil.downloadFile(org.apache.commons.io.IOUtils.toByteArray());
        } catch (IOException e) {
            throw new RuntimeException("文件下载失败");
        }
    }*/

    public void downloadFileByPath( String path) {
        try {
            InputStream input = minioStorageService.downloadFileByPath(path);
            byte[] bytes = org.apache.commons.io.IOUtils.toByteArray(input);
            FileDownloadUtil.downloadFile(bytes,"测试","");
        } catch (IOException e) {
            throw new RuntimeException("文件下载失败");
        }
    }

    public String getObjectUrl() {
       return minioStorageService.getUrl();
    }
}
